package com.example.administrator.produceqr;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24 0024.
 */

public class PhoneActivity extends AppCompatActivity {
    private RecyclerView phoneRecy;
    private PhoneAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phone_number);

        List<ContactInfo> list = getContactsInfo(this);
        phoneRecy = (RecyclerView) findViewById(R.id.phoneRecy);
        phoneRecy.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PhoneAdapter(this, list);
        phoneRecy.setAdapter(adapter);
    }

    public static List<ContactInfo> getContactsInfo(Context context) {
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, null);

        List<ContactInfo> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            ContactInfo info = new ContactInfo();
            info.name = name;
            info.phone = number;
            list.add(info);
        }
        cursor.close();

        return list;
    }
}
